package com.javacv.plus.extensions.audio;

import com.javacv.plus.core.config.ProcessorConfig;
import com.javacv.plus.core.processor.MediaProcessor;
import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.extensions.audio.config.AudioConfig;
import com.javacv.plus.extensions.audio.steps.*;
import lombok.extern.slf4j.Slf4j;

/**
 * 音频处理器
 * 提供音频处理的链式调用API
 */
@Slf4j
public class AudioProcessor extends MediaProcessor<AudioProcessor, AudioResult> {

    protected AudioProcessor(ProcessorConfig config) {
        super(config);
    }

    /**
     * 创建音频处理器
     */
    public static AudioProcessor of(String audioPath) {
        AudioConfig config = new AudioConfig();
        config.setInputPath(audioPath);
        
        AudioProcessor processor = new AudioProcessor(config);
        processor.addStep(new LoadAudioStep());
        return processor;
    }

    /**
     * 创建音频处理器（自定义配置）
     */
    public static AudioProcessor of(String audioPath, AudioConfig config) {
        config.setInputPath(audioPath);
        
        AudioProcessor processor = new AudioProcessor(config);
        processor.addStep(new LoadAudioStep());
        return processor;
    }

    /**
     * 调整音量
     */
    public AudioProcessor volume(double factor) {
        return addStep(new VolumeStep(factor));
    }

    /**
     * 调整音频速度
     */
    public AudioProcessor speed(double factor) {
        return addStep(new SpeedStep(factor));
    }

    /**
     * 裁剪音频
     */
    public AudioProcessor trim(double startSeconds, double durationSeconds) {
        return addStep(new AudioTrimStep(startSeconds, durationSeconds));
    }

    /**
     * 添加淡入效果
     */
    public AudioProcessor fadeIn(double durationSeconds) {
        return addStep(new FadeStep(FadeStep.FadeType.IN, durationSeconds));
    }

    /**
     * 添加淡出效果
     */
    public AudioProcessor fadeOut(double durationSeconds) {
        return addStep(new FadeStep(FadeStep.FadeType.OUT, durationSeconds));
    }

    /**
     * 添加回声效果
     */
    public AudioProcessor echo(double delay, double decay) {
        return addStep(new EchoStep(delay, decay));
    }

    /**
     * 添加混响效果
     */
    public AudioProcessor reverb(double roomSize, double damping) {
        return addStep(new ReverbStep(roomSize, damping));
    }

    /**
     * 调整音调
     */
    public AudioProcessor pitch(double factor) {
        return addStep(new PitchStep(factor));
    }

    /**
     * 添加均衡器
     */
    public AudioProcessor equalizer(double[] bands) {
        return addStep(new EqualizerStep(bands));
    }

    /**
     * 压缩音频
     */
    public AudioProcessor compress(String quality) {
        return addStep(new AudioCompressStep(quality));
    }

    /**
     * 转换音频格式
     */
    public AudioProcessor convertFormat(String format) {
        getConfig().setProperty("outputFormat", format);
        return this;
    }

    /**
     * 保存音频
     */
    public AudioResult save(String outputPath) {
        getConfig().setOutputPath(outputPath);
        addStep(new SaveAudioStep());
        return process();
    }

    /**
     * 获取处理结果但不保存
     */
    public AudioResult get() {
        return process();
    }

    @Override
    protected AudioResult executeProcess() throws Exception {
        ProcessorContext context = new ProcessorContext();
        
        // 执行处理链
        chain.execute(this, context);
        
        // 创建结果
        AudioResult result = new AudioResult();
        result.setSourcePath(getConfig().getInputPath());
        result.setOutputPath(getConfig().getOutputPath());
        result.setSuccess(true);
        
        return result;
    }

    /**
     * 获取音频配置
     */
    public AudioConfig getAudioConfig() {
        return (AudioConfig) getConfig();
    }
} 